﻿package cn.antscript.airCMS.display 
{
	import cn.antscript.airCMS.data.Config;
	import cn.antscript.airCMS.data.UserData;
	import cn.antscript.airCMS.display.component.WaitBar;
	import cn.antscript.model.AutoURLRequest;
	import cn.antscript.model.SqlModel;
	import com.adobe.crypto.MD5;
	import com.bit101.components.CheckBox;
	import com.bit101.components.InputText;
	import com.bit101.components.Label;
	import com.bit101.components.Panel;
	import com.bit101.components.PushButton;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.net.SharedObject;
	import flash.net.URLVariables;
	
	/**
	 * ...
	 * @author 
	 */
	public class LoginPanel extends Panel
	{
		private var _userLabel:Label;
		private var _passLabel:Label;
		private var _alertLabel:Label;
		private var _userTxt:InputText;
		private var _passTxt:InputText;
		private var _rember:CheckBox;
		private var _loginBtn:PushButton;
		private var _exitBtn:PushButton;
		
		
		
		
		public function LoginPanel() 
		{
			super();
			
			addEventListener(Event.ADDED_TO_STAGE, onAdd);		
		
		}
		
		
		private function onAdd(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, onAdd);
			
			
			
			setSize(300, 250);
			color = 0x333333;
			
			_userLabel = new Label(this, 60, 70, "用户名");
			_passLabel = new Label(this, 60, 100, "密  码");
			_alertLabel = new Label(this, 60, 160, "");
			_alertLabel.setColor(0xff0000);
			
			_userTxt = new InputText(this, 130, 70);
			_passTxt = new InputText(this, 130, 100);
			_passTxt.password = true;
			
			_rember = new CheckBox(this, 130, 140, "记住密码");
			
			_loginBtn = new PushButton(this, 40, 200, "登  录");
			_exitBtn = new PushButton(this, 160, 200, "退  出");
			
			_loginBtn.addEventListener(MouseEvent.CLICK, onLogin);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onLogin);
			_exitBtn.addEventListener(MouseEvent.CLICK, onExit);
			
			
			
			//读取shareObject
			var shareObject:SharedObject = SharedObject.getLocal(Config.SHARE_NAME);
			if (shareObject.data.user != undefined && shareObject.data.user != null) {
				_userTxt.text = shareObject.data.user;
			}
			if (shareObject.data.pass != undefined && shareObject.data.pass != null) {
				_passTxt.text = shareObject.data.pass;
				_rember.selected = true;
			}
			
			addEventListener(Event.REMOVED_FROM_STAGE, onRemove);
		}
		
		private function onRemove(e:Event):void 
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, onRemove);
			
		}
		
		private function onLogin(e:Event):void 
		{
			if (e.type==MouseEvent.CLICK || (e.type == KeyboardEvent.KEY_DOWN && e["keyCode"]==13)) {
				
				if (_userTxt.text.length <= 0 || _passTxt.text.length <= 0) {
					_alertLabel.text = "请填写完整！";
				}
				else {
					
					UserData.USER = _userTxt.text;
					UserData.PASSWORD = _passTxt.text;	
					
					WaitBar.getInstance().show(this);				
					SqlModel.getInstance().login(login);
				}			
			
			}
			
		}		
		private function login(isPass:Boolean):void 
		{
			
			
			WaitBar.getInstance().hide();
			
			
			if (isPass) {
				var shareObject:SharedObject = SharedObject.getLocal(Config.SHARE_NAME);
				shareObject.data.user = _userTxt.text;					
				if (_rember.selected) {
					shareObject.data.pass = _passTxt.text;
				}
				else {
					shareObject.data.pass = null;
				}	
				//写入shareObject			
				shareObject.flush();
				
				//_alertLabel.text = "登录成功";
				
				_loginBtn.removeEventListener(MouseEvent.CLICK, onLogin);
				stage.removeEventListener(KeyboardEvent.KEY_DOWN, onLogin);
				_exitBtn.removeEventListener(MouseEvent.CLICK, onExit);
				
				dispatchEvent(new Event(Event.COMPLETE));
			}
			else {
				_alertLabel.text = "账号或密码错误！";
				_passTxt.text = "";
			}			
			
			
		}	
		
		private function onExit(e:MouseEvent):void 
		{			
			stage.nativeWindow.close();
		}
		
	}

}